package com.falling.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.falling.reggie.common.R;
import com.falling.reggie.dto.SetmealDto;
import com.falling.reggie.entity.Category;
import com.falling.reggie.entity.Dish;
import com.falling.reggie.entity.SetMeal;
import com.falling.reggie.entity.SetmealDish;
import com.falling.reggie.service.CategoryService;
import com.falling.reggie.service.SetMealService;
import com.falling.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Resource
    private SetMealService setmealService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        if (setmealService.saveWithDish(setmealDto)) return R.success("save success...");
        else return R.error("save fail...");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){
        Page<SetMeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, SetMeal::getName, name);
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);

        setmealService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        List<SetMeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            setmealDto.setCategoryName(category.getName());

            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        if (setmealService.removeWithDish(ids)) return R.success("delete success...");
        else return R.error("delete fail...");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable String status, @RequestParam List<Long> ids){
        UpdateWrapper<SetMeal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids).set("status", status);
        if (setmealService.update(updateWrapper)) return R.success("update status success...");
        else return R.error("update status fail...");
    }

    @GetMapping("/{id}")
    public R<SetMeal> revealInfo(@PathVariable Long id){
        SetMeal setMeal = setmealService.getByIdWithDish(id);
        if (setMeal != null) return R.success(setMeal);
        else return R.error("reveal info fail...");
    }

    @GetMapping("/list")
    public R<List<SetMeal>> list(SetMeal setMeal){
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMeal::getCategoryId, setMeal.getCategoryId());
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);

        List<SetMeal> list = setmealService.list(queryWrapper);

        return R.success(list);

    }

    @GetMapping("/dish/{id}")
    public R<SetmealDto> detailOfSetMeal(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        setmealDishService.list(queryWrapper);

        return null;

    }








}
